const { Controller } = require('egg');

const validate = require('../rules/home');

class HomeController extends Controller {

  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }


  /**
   *
   * @api { GET } /config/system/isInit 是否初始化
   * @apiName isInit
   * @apiGroup system
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境
   *
   */
  async isInit() {
    const ctx = this.ctx;
    try {
      const data = await ctx.service.index.isInit();
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { POST } /config/system/sendCode 发送验证码
   * @apiName sendCode
   * @apiGroup system
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } email 邮箱
   * @apiParam { String } type 类型
   *
   */
  async sendCode() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;
      const { email, type } = await validate.sendCodeValidate(body);
      const data = await ctx.service.index.sendCode(email, type);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      console.log(error);
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { POST } /config/system/createEmail 配置邮箱
   * @apiName createEmail
   * @apiGroup system
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } email 邮箱
   * @apiParam { String } code 验证码
   *
   */
  async createEmail() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;
      const { email, code } = await validate.createEmailValidate(body);
      const data = await ctx.service.index.createEmail(email, code);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { POST } /config/system/updateEmail 更新邮箱
   * @apiName updateEmail
   * @apiGroup system
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } old_email 老邮箱
   * @apiParam { String } new_email 新邮箱
   * @apiParam { String } code 验证码
   *
   */
  async updateEmail() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;
      const { old_email, new_email, code } = await validate.updateEmailValidate(body);
      const data = await ctx.service.index.updateEmail(old_email, new_email, code);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /config/system/updatePassword 更新密码
   * @apiName updatePassword
   * @apiGroup system
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } old_email 老邮箱
   * @apiParam { String } new_email 新邮箱
   * @apiParam { String } code 验证码
   *
   */
  async updatePassword() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;
      const { password, email, code } = await validate.updatePasswordValidate(body);
      const data = await ctx.service.index.updatePassword(password, email, code);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { POST } /config/system/login 登录
   * @apiName login
   * @apiGroup system
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } code 验证码
   * @apiParam { String } login_name 登录名
   * @apiParam { String } password 密码
   *
   */
  async login() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;
      const { code, login_name, password } = await validate.loginValidate(body);
      const data = await ctx.service.index.login(code, login_name, password);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { GET } /config/system/logout 登出
   * @apiName logout
   * @apiGroup system
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   *
   */
  async logout() {
    const ctx = this.ctx;
    try {
      await ctx.service.index.logout();
      ctx.status = 200;
      ctx.sendSuccess({ data: '' });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { GET } /config/system/loginCode 登录验证码
   * @apiName loginCode
   * @apiGroup system
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   *
   */
  async loginCode() {
    const ctx = this.ctx;
    try {
      const data = await ctx.service.index.createLoginCode();
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /config/env/create 添加环境
   * @apiName create
   * @apiGroup env
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境
   *
   */
  async createEnv() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;
      const { env } = await validate.envCreateValidate(body);
      const data = await ctx.service.index.createEnv(env);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /config/env/delete 删除环境
   * @apiName delete
   * @apiGroup env
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境
   *
   */
  async deleteEnv() {
    const ctx = this.ctx;
    try {
      // const { env } = ctx.request.body;
      const { body } = ctx.request;
      const { env } = await validate.envDeleteValidate(body);

      const data = await ctx.service.index.deleteEnv(env);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      console.log(error);
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /config/env/rename 重命名环境
   * @apiName rename
   * @apiGroup env
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } oldName 老环境名称
   * @apiParam { String } newName 新环境名称
   *
   */
  async renameEnv() {
    const ctx = this.ctx;
    try {
      // const { oldName, newName } = ctx.request.body;
      const { body } = ctx.request;

      const { oldName, newName } = await validate.envRenameValidate(body);

      const data = await ctx.service.index.renameEnv(oldName, newName);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  async listEnv() {
    const ctx = this.ctx;
    try {
      const data = await ctx.service.index.listEnv();

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /config/app/create 创建应用
   * @apiName create
   * @apiGroup app
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } app 应用名称
   *
   */
  async createApp() {
    const ctx = this.ctx;
    try {

      const { body } = ctx.request;
      const { env, app } = await validate.appCreateValidate(body);
      const data = await ctx.service.index.createApp(app, env);

      ctx.status = 200;
      ctx.sendSuccess(data);
      return;
    } catch (error) {
      console.log(error);
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /config/app/delete 删除应用
   * @apiName delete
   * @apiGroup app
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } app 应用名称
   *
   */
  async deleteApp() {
    const ctx = this.ctx;
    try {

      const { body } = ctx.request;
      const { env, app } = await validate.appDeleteValidate(body);
      const data = await ctx.service.index.deleteApp(app, env);

      ctx.status = 200;
      ctx.sendSuccess(data);
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { POST } /config/app/rename 重命名应用
   * @apiName rename
   * @apiGroup app
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } oldApp 老应用名称
   * @apiParam { String } newApp 新应用名称
   *
   */
  async renameApp() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;
      const { env, oldApp, newApp } = await validate.appRenameValidate(body);
      const data = await ctx.service.index.renameApp(oldApp, newApp, env);

      ctx.status = 200;
      ctx.sendSuccess(data);
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { GET } /config/app/list 列表应用
   * @apiName list
   * @apiGroup app
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } app 应用名称
   */

  async listApp() {
    const ctx = this.ctx;
    try {

      const query = ctx.query;

      const { env } = await validate.appListValidate(query);

      const data = await ctx.service.index.listApp(env);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /config/config/create 添加配置文件
   * @apiName create
   * @apiGroup config
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } app 应用名称
   * @apiParam { Object } config 配置
   *
   */
  async createAppConfig() {
    const ctx = this.ctx;
    try {

      const { body } = ctx.request;
      const { env, app, config } = await validate.configCreate(body);
      const data = await ctx.service.index.createAppConfig(app, env, config);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /config/config/update 修改配置文件
   * @apiName update
   * @apiGroup config
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } app 应用名称
   * @apiParam { Object } config 配置
   *
   */
  async updateAppConfig() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;
      const { env, app, config } = await validate.configUpdate(body);
      const data = await ctx.service.index.updateAppConfig(app, env, config);

      ctx.status = 200;
      ctx.sendSuccess(data);
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { POST } /config/config/delete 添加配置文件
   * @apiName delete
   * @apiGroup config
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } app 应用名称
   *
   */
  async deleteAppConfig() {
    const ctx = this.ctx;
    try {

      const { body } = ctx.request;
      const { env, app } = await validate.configDelete(body);
      await ctx.service.index.deleteAppConfig(app, env);

      ctx.status = 200;
      ctx.sendSuccess({});
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { GET } /config/config/list 获取配置列表
   * @apiName list
   * @apiGroup config
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   *
   */
  async getList() {
    const ctx = this.ctx;
    try {
      const { query } = ctx;
      const { env } = await validate.envAndAppList(query);
      const data = await ctx.service.index.getList(env);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { GET } /config/config/info 获取配置详情(解密后)
   * @apiName info
   * @apiGroup config
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } app 应用名称
   *
   */
  async getInfo() {
    const ctx = this.ctx;
    try {
      const { query } = ctx;
      const { env, app } = await validate.configInfo(query);
      const data = await ctx.service.index.getInfo(app, env);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { GET } /config/sm/info 获取sm配置详情
   * @apiName info
   * @apiGroup sm
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   *
   */
  async getSM() {
    const ctx = this.ctx;
    try {

      const data = await ctx.service.index.getSM();
      ctx.status = 200;
      ctx.sendSuccess(data);
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { GET } /config/decrypt/info app获取配置文件
   * @apiName info
   * @apiGroup decrypt
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } env 环境名称
   * @apiParam { String } app 应用名称
   *
   */
  async appGetInfo() {
    const ctx = this.ctx;
    try {
      const { query } = ctx;
      const { env, app } = await validate.smConfigInfo(query);
      const data = await ctx.service.index.appGetInfo(app, env);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  async healthCheck() {
    const ctx = this.ctx;
    try {
      const msg = ctx.request.body;
      const data = await ctx.service.index.healthCheck(msg);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = HomeController;
